﻿using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Freshbox2._0.BLL
{
   public class AdminBll:BaseBll<admin>
    {
       public override DAL.BaseDal<admin> dal
       {
           get
           {
               return new DAL.BaseDal<admin>();
           }

       }
       public class LoginModel
       {
           [Required]//用户名
           public string code { get; set; }
           [Required]//密码
           public string pwd { get; set; }
           [Required]//验证码
           public string yanZheng { get; set; }

       }
       public admin GetUserByNameAndPwd(LoginModel model)
       {
           string pwd = MD5Encrypt32(model.pwd);
           Expression<Func<admin, bool>> where = x => x.adminUsername == model.code && x.adminPassword == pwd && x.state == 1;
           return dal.Search(where).FirstOrDefault();
       }

       //MD5加密方法  32位加密
       public static string MD5Encrypt32(string password)
       {
           string cl = password;
           string pwd = "";
           MD5 md5 = MD5.Create(); //实例化一个md5对像
           // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
           byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
           // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
           for (int i = 0; i < s.Length; i++)
           {
               // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
               pwd = pwd + s[i].ToString("x2");
           }
           return pwd;
       }
    }
}
